//
//  MineLeftButtonMoveViewController.m
//  02-徙游记
//
//  Created by fanzhuxian on 2017/7/16.
//  Copyright © 2017年 FanZhuxian. All rights reserved.
//

#import "MineLeftButtonMoveViewController.h"
#import "SZKImagePickerVC.h"
#import "ClearCache.h"
#import "MineOpenSourceViewController.h"
#import "MineFeedBackViewViewController.h"
#import "MineAboutPageViewViewController.h"


#define Screen_width [[UIScreen mainScreen] bounds].size.width
#define Screen_height [[UIScreen mainScreen] bounds].size.height

@interface MineLeftButtonMoveViewController ()<UITableViewDelegate ,UITableViewDataSource> {
    
    UITableView * _tableView;
}

@end

@implementation MineLeftButtonMoveViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"设置";
    
    [self initTable];
}

#pragma mark - 初始化
- (void)initTable {
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    
    _tableView .delegate = self;
    _tableView.dataSource = self;
    _tableView.showsHorizontalScrollIndicator = NO;
    
    [self.view addSubview:_tableView];
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 1;
    } else if(section == 1) {
        return 4;
    } else return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString * kCell = [NSString stringWithFormat:@"cellID%ld%ld", (long)[indexPath section], (long)[indexPath row]];
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:kCell];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCell];
    }
    
    NSInteger count = indexPath.section + indexPath.row;
    NSArray * titleText = @[@"清除缓存", @"应用评分", @"关于我们", @"开元许可", @"问题反馈", @"注销登录"];
    
    if (indexPath.section == 2) {
        
        cell.textLabel.text = @"注销登录";
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.textColor = [UIColor redColor];
    
    } else {
    
        cell.textLabel.text = titleText[count];
        cell.textLabel.textAlignment = NSTextAlignmentLeft;
        cell.textLabel.textColor = [UIColor blackColor];
    }
    
    cell.separatorInset = UIEdgeInsetsZero;
    cell.layoutMargins = UIEdgeInsetsZero;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 3;
}

#pragma mark = tableViewDelegate
// cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 45;
}

// header高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return 10;
}

// footer高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

    return 15;
}

// cell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"%ld%ld", (long)indexPath.section, (long)indexPath.row);
    
    // 关于我们
    if (indexPath.section == 1 && indexPath.row == 1) {
        MineAboutPageViewViewController * VC = [[MineAboutPageViewViewController alloc] init];
        
        [self.navigationController pushViewController:VC animated:YES];
    }
    
    // 开源许可
    if (indexPath.section == 1 && indexPath.row == 2) {
        MineOpenSourceViewController * VC = [[MineOpenSourceViewController alloc] init];
        
        [self.navigationController pushViewController:VC animated:YES];
    }
    
    // 建议
    if (indexPath.section == 1 && indexPath.row == 0) {
        MineFeedBackViewViewController * VC = [[MineFeedBackViewViewController alloc] init];
        
        [self.navigationController pushViewController:VC animated:YES];
    }
    
    // 应用评分
    if (indexPath.section == 1 && indexPath.row == 0) {
        NSDictionary * options = @{UIApplicationOpenURLOptionUniversalLinksOnly : @NO};
        
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://itunes.apple.com/cn/app/chan-you-ji-2014nian-lu-xing/id559653959?mt=8"] options:options completionHandler:nil];
    }
    
    // 清除缓存
    if (indexPath.section == 0) {
        [self alter];
    }
    
    // 退出登录
    if (indexPath.section == 2) {
        [self quiet];
    }
    
}
#pragma makr - 退出点击事件
- (void)quiet{
    
    UIAlertController * alert =[UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"您确定要退出吗?"] preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction * sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
      
        //清除头像
        [SZKImagePickerVC clearHeaderImg];
       
        //清除缓存
        [ClearCache removedCache];
       
        //添加通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeImg" object:nil];
       
        dispatch_async(dispatch_get_main_queue(), ^{
            [_tableView reloadData];
        });
    }];
   
    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
   
    [alert addAction:sureAction];
    [alert addAction:cancelAction];
    [self.navigationController presentViewController:alert animated:YES completion:nil];
    
}

#pragma mark - alter提示方法
- (void)alter {

    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"您确定要清空 %.2f M缓存么", [ClearCache clearCache]] preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction * sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        // 清除头像
        [SZKImagePickerVC clearHeaderImg];
        // 清除缓存
        [ClearCache removedCache];
        // 添加通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeImg" object:nil];
        dispatch_async(dispatch_get_main_queue(), ^{
            [_tableView reloadData];
        });
    }];
    
    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [alert addAction:sureAction];
    [alert addAction:cancelAction];
    
    [self.navigationController presentViewController:alert animated:YES completion:nil];
    
    
}

@end
